package net.anzhuo.controller.db.syncdata;



import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import net.anzhuo.common.utils.DateUtil;
import net.anzhuo.common.utils.RandomGUID;
import net.anzhuo.entity.GsBase;
import net.anzhuo.entity.GsGonggao;
import net.anzhuo.entity.GsPriority;
import net.anzhuo.entity.GsQuery;
import net.anzhuo.entity.db.syncdata.CourtCaseInfo;
import net.anzhuo.entity.db.syncdata.CourtCountResult;
import net.anzhuo.entity.db.syncdata.PageUtilsCourtCase;
import net.anzhuo.entity.db.syncdata.Paging;
import net.anzhuo.entity.db.syncdata.QueryGonggaoResult;
import net.anzhuo.entity.db.syncdata.Result;
import net.anzhuo.service.GsBaseService;
import net.anzhuo.service.GsGonggaoService;
import net.anzhuo.service.GsPriorityService;
import net.anzhuo.service.GsQueryService;

@RestController
@Slf4j
public class CourtInfoController {



	@Autowired
	private GsGonggaoService gsGonggaoService;
	
	
	@Autowired
	private GsPriorityService gsPriorityService;
	
	
	@Autowired
	private GsBaseService gsBaseService;
	
	
	@Autowired
	private GsQueryService gsQueryService;
	
	@RequestMapping("/querycourtcaseinfo")
	public QueryGonggaoResult querycourtcaseinfo(String keyNo,String date,String pageIndex, Integer pageSize) {
		log.info("===>querycourtcaseinfo="+keyNo);
		long startTime=System.currentTimeMillis();
		/*try {
			DetailResult dr = (DetailResult)RedisUtil.get("detail:"+keyNo);
			if (dr != null) {
				 long endTime=System.currentTimeMillis();
				 float excTime=(float)(endTime-startTime)/1000;
				 log.info(" getDetail_keyword 执行时间："+excTime+"s");
				log.info("===>getDetail_keyword= redis is hitted!");
				return dr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		QueryGonggaoResult detailResult = new QueryGonggaoResult();
		try {
	
		detailResult.setStatus("200");
		detailResult.setMessage("查询成功");
		GsGonggao p=new GsGonggao();
		p.setCoMd5(keyNo);
		p.setUpdateTime(date);
		List<GsGonggao> listp=gsGonggaoService.getByUpdateTime(p);
		Paging paging = new Paging();
		paging.setPageIndex(Integer.parseInt(pageIndex));
		paging.setPageSize(pageSize);
		
		paging.setTotalRecords(listp.size());
		
		
		detailResult.setPaging(paging);
		List<CourtCaseInfo> listPenalty=new ArrayList<CourtCaseInfo>();
		if(listp!=null)
		{
			CourtCaseInfo pl=new CourtCaseInfo();
			for(GsGonggao g:listp)
			{
				pl=new CourtCaseInfo();
				pl.setCaseNo(g.getCaseNo());
				pl.setCaseName(g.getCaseName());
				pl.setContent(g.getContent());
				pl.setCourtName(g.getCourt());
				pl.setDecisionDate(g.getJudgeDate());
				pl.setJudgePro(g.getTrialRound());
				pl.setUpdateTime(g.getUpdateTime());
				pl.setDocInfo(g.getDocInfo());
			    pl.setCaseType(g.getCaseType());		
				pl.setCaseRole(g.getCaseRole());
				listPenalty.add(pl);
			}
		}
		
        PageUtilsCourtCase pup=new PageUtilsCourtCase(listPenalty,paging.getPageIndex().intValue(),paging.getPageSize().intValue());
		
		detailResult.setCourtCaseInfo(pup.result);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	
		
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" querycourtcaseinfo 执行时间："+excTime+"s");
		return detailResult;
	}
	
	
	@RequestMapping("/querycourtcaseinforealtime")
	public QueryGonggaoResult querycourtcaseinforealtime(String searchKey) {
		log.info("===>querycourtcaseinforealtime="+searchKey);
		long startTime=System.currentTimeMillis();
		/*try {
			DetailResult dr = (DetailResult)RedisUtil.get("detail:"+keyNo);
			if (dr != null) {
				 long endTime=System.currentTimeMillis();
				 float excTime=(float)(endTime-startTime)/1000;
				 log.info(" getDetail_keyword 执行时间："+excTime+"s");
				log.info("===>getDetail_keyword= redis is hitted!");
				return dr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		QueryGonggaoResult detailResult = new QueryGonggaoResult();
		try {
		List<GsBase>	listBase=gsBaseService.getByNameList(searchKey);
		detailResult.setStatus("200");
		detailResult.setMessage("查询成功");
		GsBase gsBase=new GsBase();
		if(listBase.size()>0)
		{
		gsBase=listBase.get(0);
		
		}else//公司不存在
		{
			GsQuery gsQuery=new GsQuery();
			gsQuery.setName(searchKey);
			List<GsQuery> listQuery=gsQueryService.getBycoMd5(gsQuery);
			if(listQuery.size()==0)
			{
				gsQuery.setStatus("0");
				gsQuery.setKeyNo(RandomGUID.generatorGUID());
				gsQueryService.save(gsQuery);
			}
			//waiting for update
			listBase=gsBaseService.getByNameList(searchKey);
			int i=0;
			while(listBase.size()==0&&i<1000)
			{
				listBase=gsBaseService.getByNameList(searchKey);
				i++;
				Thread.currentThread().sleep(10000);
			}
		}
		listBase=gsBaseService.getByNameList(searchKey);
		if(listBase.size()>0)
		{
		gsBase=listBase.get(0);
		
		}
		GsGonggao p=new GsGonggao();
		p.setCoMd5(gsBase.getMd5());
		
		boolean result=false;
		GsPriority gp=new GsPriority();
		gp.setKeyNo(gsBase.getMd5());
		gp.setOperdate(DateUtil.getDate("yyyy-MM-dd"));
		gp.setStatus("0");
		List<GsPriority> list=gsPriorityService.getByKeyNo(gp);
		if(list.size()==0)
		{
		gsPriorityService.save(gp);
		}else
		{
			gp.setId(list.get(0).getId());
			gsPriorityService.update(gp);
		}
		while(!result)
		{
			list=gsPriorityService.getByKeyNo(gp);
			for(GsPriority g:list)
			{
				String status=g.getStatus();
				if(status.equals("1"))
				{
					result=true;
				}
			}
		}
		p.setCoMd5(gsBase.getMd5());
		List<GsGonggao> listp=gsGonggaoService.getByCoMd5(p.getCoMd5());
		
		List<CourtCaseInfo> listPenalty=new ArrayList<CourtCaseInfo>();
		if(listp!=null)
		{
			CourtCaseInfo pl=new CourtCaseInfo();
			for(GsGonggao g:listp)
			{
				pl=new CourtCaseInfo();
				pl.setCaseNo(g.getCaseNo());
				pl.setCaseName(g.getCaseName());
				pl.setContent(g.getContent());
				pl.setCourtName(g.getCourt());
				pl.setDecisionDate(g.getJudgeDate());
				pl.setJudgePro(g.getTrialRound());
				pl.setUpdateTime(g.getUpdateTime());
				pl.setCaseRole(g.getCaseRole());
				listPenalty.add(pl);
			}
		}
		
      
		detailResult.setCourtCaseInfo(listPenalty);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	
		
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" querycourtcaseinforealtime 执行时间："+excTime+"s");
		return detailResult;
	}
	
	
	@RequestMapping("/courtcaseinfocount")
	public CourtCountResult courtcaseinfocount(String keyNo) {
		log.info("===>courtcaseinfocount="+keyNo);
		long startTime=System.currentTimeMillis();
		/*try {
			DetailResult dr = (DetailResult)RedisUtil.get("detail:"+keyNo);
			if (dr != null) {
				 long endTime=System.currentTimeMillis();
				 float excTime=(float)(endTime-startTime)/1000;
				 log.info(" getDetail_keyword 执行时间："+excTime+"s");
				log.info("===>getDetail_keyword= redis is hitted!");
				return dr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		CourtCountResult detailResult = new CourtCountResult();
		detailResult.setStatus("200");
		detailResult.setMessage("courtcaseinfocount success");
		GsGonggao  p=new GsGonggao();
		
		p.setCoMd5(keyNo);
	
		List<GsGonggao> list=gsGonggaoService.getByUpdateTime(p);
		detailResult.setCount(list.size());
	
		
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" courtcaseinfocount 执行时间："+excTime+"s");
		return detailResult;
	}
	
	
	@RequestMapping("/deletecourtcaseinfo")
	public Result updatecourtcaseinfo(String keyNo) {
		log.info("===>deletecourtcaseinfo="+keyNo);
		long startTime=System.currentTimeMillis();
		/*try {
			DetailResult dr = (DetailResult)RedisUtil.get("detail:"+keyNo);
			if (dr != null) {
				 long endTime=System.currentTimeMillis();
				 float excTime=(float)(endTime-startTime)/1000;
				 log.info(" getDetail_keyword 执行时间："+excTime+"s");
				log.info("===>getDetail_keyword= redis is hitted!");
				return dr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		Result detailResult = new Result();
		detailResult.setStatus("200");
		detailResult.setMessage("delete courtcaseinfo success");
		GsGonggao  p=new GsGonggao();
		
		p.setCoMd5(keyNo);
	
		gsGonggaoService.delete(p);
		
	
		
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" deletecourtcaseinfo 执行时间："+excTime+"s");
		return detailResult;
	}
	
	
	
	@RequestMapping(value="/updatecourtcaseinfo",method=RequestMethod.POST)
	public Result updatecourtcaseinfo(String caseNo,String caseName,String courtName,String content,String decisionDate,String judgePro,String updateTime,
			String keyNo,String docId,String contentInfo,String caseRole,
			String defendantlist,
			String province,
			String prosecutorlist,
			String docInfo,
			String caseType,
			String publicDate) {
		log.info("===>updatecourtcaseinfo="+keyNo);
		long startTime=System.currentTimeMillis();
		/*try {
			DetailResult dr = (DetailResult)RedisUtil.get("detail:"+keyNo);
			if (dr != null) {
				 long endTime=System.currentTimeMillis();
				 float excTime=(float)(endTime-startTime)/1000;
				 log.info(" getDetail_keyword 执行时间："+excTime+"s");
				log.info("===>getDetail_keyword= redis is hitted!");
				return dr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		Result detailResult = new Result();
		try {
	
		detailResult.setStatus("200");
		detailResult.setMessage("插入数据成功");
		GsGonggao  p=new GsGonggao();
		p.setCaseNo(caseNo);
		p.setCaseName(caseName);
		p.setContent(content);
		p.setJudgeDate(decisionDate);
		p.setNoticeTime(publicDate);
		p.setNoticeType("裁判文书");
		p.setCourt(courtName);
		p.setCoMd5(keyNo);
		p.setTrialRound(judgePro);
		p.setUpdateTime(updateTime);
		p.setDocId(docId);
		p.setCaseRole(caseRole);
		p.setDefendantlist(defendantlist);
		p.setDefendant(defendantlist);
		p.setProsecutorlist(prosecutorlist);
		p.setProvince(province);
		p.setMd5(RandomGUID.generatorGUID());
		p.setDocInfo(docInfo);
		p.setCaseType(caseType);
		List<GsGonggao> listp=gsGonggaoService.getByCoMd5(keyNo);
		boolean b=false;
		if(listp!=null)
		{
			for(GsGonggao g:listp)
			{
				if(g.getDocId()!=null)
				{
				  if(g.getDocId().equals(p.getDocId()))
				  {
					  b=true;
					  break;
				  }
				}
			}
		}
		if(!b)
		{
			gsGonggaoService.save(p);
		}		
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	
		
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" updatecourtcaseinfo 执行时间："+excTime+"s");
		return detailResult;
	}
	
	
}
